package cn.edu.nuc.ssm.controller;

import cn.edu.nuc.ssm.dto.BbsReplyAndUsername;
import cn.edu.nuc.ssm.dto.BbsTopicAndUsername;
import cn.edu.nuc.ssm.dto.Result;
import cn.edu.nuc.ssm.dto.SecondBbsReblyAndRAndToUsername;
import cn.edu.nuc.ssm.entity.BbsReply;
import cn.edu.nuc.ssm.entity.BbsSection;
import cn.edu.nuc.ssm.entity.BbsTopic;
import cn.edu.nuc.ssm.entity.User;
import cn.edu.nuc.ssm.service.BbsRelpyService;
import cn.edu.nuc.ssm.service.BbsSectionService;
import cn.edu.nuc.ssm.service.BbsTopicService;
import cn.edu.nuc.ssm.service.UserService;
import cn.edu.nuc.ssm.util.Pager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/4/19.
 */
@Controller
@RequestMapping("/bbs")
public class BbsControllor {

    private static Logger logger = LoggerFactory.getLogger(BbsControllor.class);
    @Autowired
    private BbsTopicService bbsTopicService;
    @Autowired
    private BbsSectionService bbsSectionService;
    @Autowired
    private BbsRelpyService bbsRelpyService;
    @Autowired
    private UserService userService;



    @RequestMapping(value = "/towritetopic",method = RequestMethod.GET)
    public String  towriteTopic(HttpSession session,Model model){

        User user1 = (User) session.getAttribute("user");
        if (user1==null){
            return "redirect:/front/tologin";
        }


        List<String> allSectionType = bbsSectionService.findAllSectionType();

        List<BbsSection> list = bbsSectionService.find();

        model.addAttribute("allsection",allSectionType);
        model.addAttribute("subsection",list);

        User user = (User) session.getAttribute("user");
        if (user==null){
            return "redirect:/front/tologin";
        }
        List<String> sectionType = bbsSectionService.findSectionType();
        model.addAttribute("sectionlist",sectionType);
        return "front/bbs/bbs_write_topic";
    }
    @ResponseBody
    @RequestMapping(value = "/writetopic",method = RequestMethod.POST)
    public Result<BbsTopic> writeTopic(@RequestBody BbsTopic bbsTopic, Model model,HttpSession session){



        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String nowTime = simpleDateFormat.format(date);
        bbsTopic.setTime(nowTime);
        bbsTopic.setClickCount(0);
        logger.debug("接收的参数title是:{}" , bbsTopic);

        int i = 0;
        try {
            i = bbsTopicService.insertBbsTopic(bbsTopic);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (i<=0){
            return new Result<BbsTopic>("error",null);
        }
        logger.debug("返回的主键id：{}",bbsTopic.getId());
        return new Result<BbsTopic>("success",bbsTopic);
    }

    @RequestMapping(value = "/opentopic",method = RequestMethod.GET)
    public String openTopic(BbsTopic bbsTopic,Model model,@RequestParam(value = "currentPage",defaultValue = "1",required = false) int currentPage){

        List<String> allSectionType = bbsSectionService.findAllSectionType();

        List<BbsSection> list = bbsSectionService.find();

        model.addAttribute("allsection",allSectionType);
        model.addAttribute("subsection",list);

        BbsTopic bbsTopicExist = bbsTopicService.findbyId(bbsTopic.getId());

        User topicuser = userService.findbyId(bbsTopicExist.getUid());

        model.addAttribute("topicuser",topicuser);

        logger.debug("当前页是:{}",currentPage);
        logger.debug("查询的数据:{}",bbsTopicExist);

        Pager firstReplyPage = bbsRelpyService.findPageList(currentPage, bbsTopicExist.getUid(), bbsTopicExist.getSid(), bbsTopicExist.getId(), 1);
        model.addAttribute("firstReplyPage",firstReplyPage);

      //  List<BbsReply> replySecondList = bbsRelpyService.findbytidandlevel(bbsTopic.getId());

        List<SecondBbsReblyAndRAndToUsername> replySecondList = bbsRelpyService.findbytidandlevel(bbsTopic.getId());

        logger.debug("SecondBbsReblyAndRAndToUsername的值J:{}",replySecondList);

        logger.debug("replySecondList  {}",replySecondList);
        model.addAttribute("secondReplylist",replySecondList);

        model.addAttribute("topic",bbsTopicExist);
        logger.debug("open bbs是:{}",bbsTopicExist);


        //更新浏览量
        bbsTopicService.addViewCount(bbsTopic.getId());


        return "front/bbs/bbs_open_topic";
    }

    @ResponseBody
    @RequestMapping(value = "/get2classtype",method = RequestMethod.POST)
    public List<BbsSection> get2SectionType(String sectionType){

        return bbsSectionService.findbySectionType(sectionType);
    }


    @RequestMapping(value = "/writereplyfirst",method = RequestMethod.POST)
    public String writeFirstReply(BbsReply record,HttpSession session,Model model){

        User user = (User) session.getAttribute("user");
        if (user==null){
            return "redirect:/front/tologin";
        }
        logger.debug("接收的参数:{}",record);

        List<String> allSectionType = bbsSectionService.findAllSectionType();

        List<BbsSection> list = bbsSectionService.find();

//        model.addAttribute("allsection",allSectionType);
//        model.addAttribute("subsection",list);


        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String nowTime = simpleDateFormat.format(date);
        record.setReplyTime(nowTime);
        record.setClickCount(0);
        record.setLevel(1);

        int insert = bbsRelpyService.insert(record);

        if (insert<= 0){

            model.addAttribute("msg","数据发生错误");
            return "front/error/error";
        }

        logger.debug(":{}",record);


        return "redirect:/bbs/opentopic?id="+record.getTid();
    }

    @RequestMapping(value = "/towritereplysecond",method = RequestMethod.GET)
    public String replyFirst(int id,Model model,HttpSession session){

        User user1 = (User) session.getAttribute("user");
        if (user1==null){
            return "redirect:/front/tologin";
        }


        List<String> allSectionType = bbsSectionService.findAllSectionType();
        List<BbsSection> list = bbsSectionService.find();
        model.addAttribute("allsection",allSectionType);
        model.addAttribute("subsection",list);
        BbsReplyAndUsername bbsReply = bbsRelpyService.findbyIdandFirstlevel(id);
        model.addAttribute("bbsReply",bbsReply);
        BbsTopic bbsTopicExist = bbsTopicService.findbyId(bbsReply.getTid());
        model.addAttribute("topic",bbsTopicExist);
        return "front/bbs/bbs_write_secondReply";
    }



    @ResponseBody
    @RequestMapping(value = "/writereplysecond",method = RequestMethod.POST)
    public Result<BbsReply> replysecond(@RequestBody BbsReply bbsReply,Model model){




        List<String> allSectionType = bbsSectionService.findAllSectionType();
        List<BbsSection> list = bbsSectionService.find();
        model.addAttribute("allsection",allSectionType);
        model.addAttribute("subsection",list);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String nowTime = simpleDateFormat.format(date);
        bbsReply.setReplyTime(nowTime);
        int i = bbsRelpyService.insertBbsReply2Level(bbsReply);
        if (i<=0){
            return new Result<>("error",null);
        }
        model.addAttribute("bbsReply",bbsReply);
        return new Result<BbsReply>("success",bbsReply);
    }


    @RequestMapping(value = "/opensection",method = RequestMethod.GET)
    public String opensectiontopiclist(int sid,Model model){

        List<String> allSectionType = bbsSectionService.findAllSectionType();
        List<BbsSection> list = bbsSectionService.find();
        model.addAttribute("allsection",allSectionType);
        model.addAttribute("subsection",list);

        List<BbsTopicAndUsername> topicAndUsername = bbsTopicService.findTopicAndUsername(sid);

        model.addAttribute("topiclist",topicAndUsername);

        BbsSection bbsSection = bbsSectionService.findbyId(sid);

        model.addAttribute("bbsSection",bbsSection);

        return "front/bbs/bbs_topic_list";
    }

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(Model model){

        List<String> allSectionType = bbsSectionService.findAllSectionType();
        List<BbsSection> list = bbsSectionService.find();
        model.addAttribute("allsection",allSectionType);
        model.addAttribute("subsection",list);

        List<BbsTopic> bbsTopicList = bbsTopicService.findbolocation();

        model.addAttribute("bolist",bbsTopicList);

        List<BbsTopic> bbsHotList = bbsTopicService.findhotlocation();

        model.addAttribute("hotlist",bbsHotList);

        List<BbsTopic> bbsTopichit = bbsTopicService.findhitlocation();

        model.addAttribute("hitlist",bbsTopichit);

        return "front/bbs/bbs_index";
    }




}
